package com.palnar.ilecker.manager;

import java.io.IOException;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.concurrent.TimeoutException;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.SoapFault;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.AndroidHttpTransport;
import org.xmlpull.v1.XmlPullParserException;

import com.palnar.ilecker.constants.IServiceConstants;
import com.palnar.ilecker.model.ServiceManagerModel;
import com.palnar.ilecker.utils.Utils;

import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;

public class AllServiceManager extends
		AsyncTask<Void, Void, ArrayList<SoapObject>> implements
		IServiceConstants {
	SoapWithArgsListener dataDownloadListener;

	private ArrayList<String> wsdlString;
	private ArrayList<Object> name;// , value;

	private ArrayList<Object> value;

	private ArrayList<String> soapActionForFunction;
	private ArrayList<String> method;
	private Context context;
	private CustomProgressDialog progress;

	private ArrayList<SoapObject> soapObject;
	private SoapObject soapObjectResponse;

	private boolean isException = false;
	private String exceptionMsg = "";
	
	private ArrayList<Boolean>  isArgument;

	public AllServiceManager(ArrayList<String> wsdlString,
			ArrayList<Object> name, ArrayList<Object> value,
			ArrayList<String> method, ArrayList<String> soapActionForFunction,
			CustomProgressDialog progress) {

		this.wsdlString = wsdlString;
		this.name = name;
		this.value = value;
		this.method = method;
		this.soapActionForFunction = soapActionForFunction;
		this.progress = progress;

		

	}
	public AllServiceManager(ArrayList<ServiceManagerModel> managerModel,CustomProgressDialog progress) {
		
		
		this.progress = progress;

		this.wsdlString=new ArrayList<String>();
		this.method=new ArrayList<String>();
		this.soapActionForFunction=new ArrayList<String>();
		this.value=new ArrayList<Object>();
		this.name=new ArrayList<Object>();
		this.isArgument=new ArrayList<Boolean>();
		for (ServiceManagerModel serviceManagerModel : managerModel) {
				
			
			

			this.wsdlString.add(serviceManagerModel.getWsdlString().toString());

			this.method.add(serviceManagerModel.getMethod().toString());
			this.soapActionForFunction.add(serviceManagerModel.getSoapActionForFunction().toString());
			
			this.name.add(serviceManagerModel.getName());
			this.value.add(serviceManagerModel.getValue());
			
			this.isArgument.add(serviceManagerModel.getIsArguments());

		}
			
	}

	public void setAllWithServiceListener(SoapWithArgsListener dataDownloadListener) {
		this.dataDownloadListener = dataDownloadListener;
	}

	@Override
	protected void onPreExecute() {
		super.onPreExecute();
		progress.show();
		
		soapObject=new ArrayList<SoapObject>();

		for (int i = 0; i < wsdlString.size(); i++) {
			
			Log.e("TEST", "********* "+i+" *************");
			Log.e("TEST", "**********START*************");
			Log.e("TEST", " URL= " + wsdlString.get(i));
			Log.e("TEST", " METHOD= " + method.get(i));
			Log.e("TEST", " ACTION= " + soapActionForFunction.get(i));
			Log.e("TEST", "***********END************");
			

			ArrayList<String> tempName = (ArrayList<String>) name.get(i);
			ArrayList<Object> tempValue = (ArrayList<Object>) value.get(i);

			SoapObject soapObj = new SoapObject(NAMESPACE, method.get(i));
			
			
			if(isArgument.get(i)){
			

			for (int j = 0; j < tempValue.size(); j++) {

				PropertyInfo pi = new PropertyInfo();

				pi.setName(tempName.get(j));
				pi.setValue(tempValue.get(j));
				
				System.out.println(" "+tempName.get(j)+"  "+tempValue.get(j));

				if (tempValue.get(j) instanceof String) {
					pi.setType(PropertyInfo.STRING_CLASS);
					
					System.out.println("String Class");
				} else if (tempValue.get(j) instanceof Boolean) {
					pi.setType(PropertyInfo.BOOLEAN_CLASS);
					System.out.println(" Boolean Class");
				}

				soapObj.addProperty(pi);

			}
			
			}
			
			
			
			
			soapObject.add(soapObj);
			
			
			

		}

		System.out.println("PRE...");

	}

	@Override
	protected ArrayList<SoapObject> doInBackground(Void... params) {

		ArrayList<SoapObject> soapObjects = new ArrayList<SoapObject>();
		
		
		
		
		for (int i = 0; i < wsdlString.size(); i++) {
			
	
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);

		envelope.dotNet = true;

		envelope.setOutputSoapObject(soapObject.get(i));

		AndroidHttpTransport httpTransport = new AndroidHttpTransport(wsdlString.get(i));

		httpTransport.debug = true;

		try {
			System.setProperty("http.keepAlive", "false");

			httpTransport.call(soapActionForFunction.get(i), envelope);

		} catch (Exception e) {
			isException = true;
			exceptionMsg = "1st EXp"+i;

		}

		try {

			soapObjectResponse = (SoapObject) envelope.getResponse();
			
			soapObjects.add(soapObjectResponse);
		} catch (SoapFault e) {
			e.printStackTrace();

			isException = true;
			exceptionMsg = "2nd EXp"+i;
		} catch (Exception e) {
			isException = true;
			exceptionMsg = "3rd EXp"+i;

		}
		
		}


		return soapObjects;
	}

	@Override
	protected void onPostExecute(ArrayList<SoapObject> result) {
		super.onPostExecute(result);
		progress.dismiss();

		System.out.println("POST...");
		if (isException) {

			dataDownloadListener.dataDownloadedFailed(exceptionMsg);
		} else {
			dataDownloadListener.dataDownloadedSuccessfully(result,method);

			//Utils.longInfo(result.toString());

		}

	}

	public static interface SoapWithArgsListener {

		void dataDownloadedSuccessfully(ArrayList<SoapObject> result,ArrayList<String> method);

		void dataDownloadedFailed(String error);

	}

}
